﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using DevExpress.XtraEditors;
namespace GenCode
{
    public partial class Form1 : Form
    {
        public bool yes = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void cmbAuthentication_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbAuthentication.SelectedIndex == 0)
            {
                txtUsername.Enabled = false;
                txtPassword.Enabled = false;
            }
            else
            {
                txtUsername.Enabled = true;
                txtPassword.Enabled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Quyền Windows
            #region Windows Authentication
            if (cmbAuthentication.SelectedIndex == 0)
            {
                cmbDatabase.Items.Clear();
                var con = new SqlConnection("Data Source=" + txtServer.Text + ";Initial Catalog=master;Integrated Security=True;");
                var cmd = new SqlCommand("SP_DATABASES", con);
                SqlDataReader reader;
                try
                {
                    con.Open();
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        cmbDatabase.Items.Add(reader[0].ToString());
                    }
                    cmbDatabase.Enabled = true;
                    XtraMessageBox.Show("Kết nối thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Global1.Server = txtServer.Text;
                    Global1.user = "";
                    Global1.pass = "";
                    Global1.win = true;
                    Global1.QuyenWin("master");
                    yes = true;
                    //frmMain frm = new frmMain();
                    //frm.Show();
                    //this.Visible = false;
                    this.Close();
                }
                catch
                {
                    XtraMessageBox.Show("Lỗi, vui lòng thử lại!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (con.State == ConnectionState.Open)
                        con.Close();
                    con.Dispose();
                    cmd.Dispose();
                }
            }
            #endregion
            //Quyền SQL Server
            #region Sql Server Authentication
            
            
            else if (cmbAuthentication.SelectedIndex == 1)
            {
                cmbDatabase.Items.Clear();
                var con = new SqlConnection("Data Source=" + txtServer.Text + ";Initial Catalog=master;User Id=" + txtUsername.Text + ";Password=" + txtPassword.Text + ";");
                var cmd = new SqlCommand("SP_DATABASES", con);
                SqlDataReader reader;
                try
                {
                    con.Open();
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        cmbDatabase.Items.Add(reader[0].ToString());
                    }
                    cmbDatabase.Enabled = true;
                    XtraMessageBox.Show("Kết nối thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Global1.Server = txtServer.Text;
                    Global1.user=txtUsername.Text;
                        Global1.pass=txtPassword.Text;
                        Global1.win = false;
                    Global1.QuyenAuthen("master");
                    //frmMain frm = new frmMain();
                    //frm.Show();
                    //this.Visible = false;
                    yes = true;
                    this.Close();
                }
                catch
                {
                    XtraMessageBox.Show("Lỗi, vui lòng thử lại!", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    if (con.State == ConnectionState.Open)
                        con.Close();
                    con.Dispose();
                    cmd.Dispose();
                }
            }
            #endregion
        }

        private void cmbDatabase_SelectedIndexChanged(object sender, EventArgs e)
        {
//            listBox1.Items.Clear();
//            string Query =
//                           @"SELECT [TABLE_NAME]
//                           FROM INFORMATION_SCHEMA.TABLES
//                           WHERE table_type = 'BASE TABLE'";
//            var con = new SqlConnection("Data Source=" + txtServer.Text + ";Initial Catalog="+cmbDatabase.Text+";Integrated Security=True;");
//            var cmd = new SqlCommand(Query, con);
//            SqlDataReader reader;
//            try
//            {
//                con.Open();
//                reader = cmd.ExecuteReader();
//                while (reader.Read())
//                {
//                    listBox1.Items.Add(reader[0].ToString());
//                }
//            }
//            catch(SqlException ex)
//            {
//                XtraMessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
//            }
//            finally
//            {
//                if (con.State == ConnectionState.Open)
//                    con.Close();
//                con.Dispose();
//                cmd.Dispose();
//            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            txtServer.Text = "(local)";
            cmbAuthentication.SelectedIndex = 0;
        }
 
    }
}
